package koction.model.consumer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import koction.model.admin.AskSellerVO;
import koction.model.product.ProductVO;
import koction.model.purchaseListBoard.ListVO;
import koction.model.purchaseListBoard.PagingBean;
import koction.model.seller.SellerVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ConsumerServiceImpl implements ConsumerService {
	@Resource(name="consumerDaoImpl")
	private ConsumerDao consumerDao;

	@Override
	public ConsumerVO consumerIdCheck(String id) {
		return consumerDao.findById(id);
	}

	@Override
	public int registerConsumer(ConsumerVO cvo) {
		return consumerDao.registerConsumer(cvo);
	}

	@Override
	public int updateConsumer(ConsumerVO cvo) {
		return consumerDao.updateConsumer(cvo);
	}

	@Override
	public ConsumerVO loginConsumer(ConsumerVO cvo) {
		return consumerDao.loginConsumer(cvo);
	}

	@Override
	public int deleteConsumer(ConsumerVO cvo) {
		return consumerDao.deleteConsumer(cvo);
	}

	@Transactional
	@Override
	public void puchasePoint(ConsumerVO cvo) {
		Map map=new HashMap();
		map.put("id", cvo.getId());
		map.put("point", cvo.getPoint());
		consumerDao.updatePoint(map);
		consumerDao.updateTotalPoint(map);
	}
	@Override
	public GradeVO myGrade(ConsumerVO cvo) {
		Map map=new HashMap();
		map.put("min", cvo.getTotalPoint());
		map.put("max", cvo.getTotalPoint());
		return consumerDao.myGrade(map);
	}

	@Override
	public ConsumerVO findConsumerById(String id) {
		// TODO Auto-generated method stub
		return consumerDao.findConsumerById(id);
	}

	@Transactional
	@Override
	public void buyProduct(ProductVO pvo, int amount, ConsumerVO cvo, int point) {
		int totalPrice=-point; // 감소되야할 포인트(총 구매 가격)
		System.out.println(totalPrice);
		System.out.println(pvo);
		Map map=new HashMap();
		map.put("id", cvo.getId());
		map.put("point", totalPrice);
		map.put("purchaseAmount", amount);
		map.put("productId", pvo.getProductId());
		map.put("sellerId", pvo.getSellerId());
		map.put("productName", pvo.getProductName());
		//포인트 차감
		consumerDao.updatePoint(map);
		//구매리스트에 추가
		consumerDao.registerPurchaseList(map);
		//상품 수량 감소, 판매량 증가
		consumerDao.updateProductInventory(map);
		//판매내역 리스트에 추가
		consumerDao.registerSellList(map);
	}

	@Override
	public ListVO getPurchaseList(String id, String pageNo) {
		if(pageNo==null||pageNo==""){
			pageNo="1";
		}
		int total=consumerDao.getTotalPurchaseList(id);
		Map map=new HashMap();
		map.put("id",id);
		map.put("pageNo", pageNo);
		System.out.println(pageNo+"pageNo");
		List<PurchaseListVO> list=consumerDao.getPurchaseList(map);
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		if(list.size()==0){
			if(paging.getNowPage()!=1){
				paging=new PagingBean(total,Integer.parseInt(pageNo)-1);
				map.put("pageNo", Integer.parseInt(pageNo)-1);
				list=consumerDao.getPurchaseList(map);
			}
		}
		ListVO lvo=new ListVO(list,paging);
		
		System.out.println("결과"+lvo.getList()+lvo.getPagingBean().getNowPage());
		return lvo;
	}
	
	@Override
	public ProductVO findProductById(int productId) {
		return consumerDao.findProductById(productId);
	}
	
	@Override
	public void deletePurchaseList(String[] purchaseNo) {
		for(int i=0; i<purchaseNo.length; i++){
			consumerDao.deletePurchaseList(purchaseNo[i]);
		}
	}
	@Override
	public SellerVO findSellerByConsumerId(String id) {
		return consumerDao.findSellerByConsumerId(id);
	}

	@Override
	public void askSeller(AskSellerVO asvo) {
		consumerDao.askSeller(asvo);
	}
	

	@Override
	public int updateGrade(Map map) {
		return consumerDao.updateGrade(map);
	}
	

}
